package de.koller.altitude.render;

import de.koller.aframe.gl.AFrame;
import de.koller.aframe.gl.Camera;
import de.koller.aframe.gl.FileShaderProgram;
import de.koller.altitude.ANode;
import de.koller.altitude.Altitude;
import de.koller.oceanFramework.gl.OceanGame;
import de.koller.oceanFramework.gl.util.OceanShader;
import de.koller.oceanFramework.gl.util.ViewUtil;
import de.koller.oceanFramework.tree.Child;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;

public class Render extends ANode {
	
	public final Camera camera = new Camera();
	@Child public final ATextures textures = new ATextures();
	@Child public final RenderContainer container = new RenderContainer();

	private AFrame frame = new AFrame( 800, 600 );
	private OceanShader screenshader;
	
	@Override
	protected void init() {
		super.init();

		frame.setUp();
		
		screenshader = OceanShader.createShader( new FileShaderProgram(
				"de/koller/altitude/render/shader/blur.vert", 
				"de/koller/altitude/render/shader/blur.frag" ) );
		
		GL11.glEnable( GL11.GL_CULL_FACE );
		GL11.glCullFace( GL11.GL_BACK );

		GL11.glEnable( GL11.GL_DEPTH_TEST );
		GL11.glDepthFunc( GL11.GL_LEQUAL );
		
		GL11.glAlphaFunc( GL11.GL_GREATER, 0.01f );
		GL11.glEnable( GL11.GL_ALPHA_TEST );
	}
	
	private void perspectiveSet() {
		GL11.glMatrixMode( GL11.GL_PROJECTION );
		GL11.glLoadIdentity();

		GLU.gluPerspective( 60.0f, 800f/600f, 0.001f, 2000.0f );
		
		GL11.glMatrixMode( GL11.GL_MODELVIEW );
		GL11.glLoadIdentity();
	}
	
	private void orthoSet() {
		GL11.glMatrixMode( GL11.GL_PROJECTION );
		GL11.glLoadIdentity();

		GL11.glOrtho( 0, 800, 600, 0, -1000, 1000 );
		
		GL11.glMatrixMode( GL11.GL_MODELVIEW );
		GL11.glLoadIdentity();
	}
	
	public void renderGame( int elapsedTime ) {		
		//GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_CLAMP );
		//GL11.glTexParameteri( GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_CLAMP );
	
		frame.bind();
		renderSzene();
		frame.unbind();
		renderFrame();
		
		OceanGame.checkForErrorGL( "GLError" );
	}

	private void renderSzene() {
		perspectiveSet();
		
		GL11.glPushMatrix(); 
		camera.setTo( Altitude.activePlayer().getLocation(), Altitude.activePlayer().getEyeHeight() );
		camera.transformGL();
		
//		gridshader.useShader();
		GL11.glColor3f( 1, 1, 1 );
		aroot.engine.floor.renderThis();
	
		//textures.gscreen.getRenderer().setCurrentBackColor( 0xffff0000 );
		//textures.gscreen.getRenderer().setCurrentFontColor( 0xffffff00 );
		//textures.gscreen.getRenderer().renderString( 0, 0, "Fabian" );
		//textures.gscreen.updateBuffer();
		//textures.gscreen.getTexture().bind();
		//ViewUtil.draw2D( 2, 2, 2, 2, 5, .1f, .1f, 0, 0 );
		
//		gridshader.unuseShader();
				
		GL11.glPopMatrix();
	}

	private void renderFrame() {
		orthoSet();
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_STENCIL_BUFFER_BIT);
		GL11.glColor3f( 1, 1, 1 );
		frame.renderScreen();
		
		renderCrosshair();
	}
	
	private void renderCrosshair() {
		GL11.glEnable( GL11.GL_COLOR_LOGIC_OP );
		GL11.glLogicOp( GL11.GL_XOR );
		
		textures.cross.bind();
		ViewUtil.draw2D( 396, 296, 8, 8, 1 );
		
		GL11.glDisable( GL11.GL_COLOR_LOGIC_OP );
	}

}
